/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.rhythm.commons.io;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

/**
 *
 * @author Michael J. Lee
 */
public class ClassPath extends File
{
    public static final long serialVerisonUID=1;
    
 
    
    /**
     * Factory method for loading a given class path resource into a 
     * <code>java.io.File</code>
     * 
     * @param classPath java class path<br>
     * <strong>EXAMPLE:</strong><br>
     * "com.yourModifiedPackage.yourClass.java"<br> 
     * "com.yourModifiedPackage.yourXML.xml<br><br>
     * <strong>NOTE:</strong>Alternativly you can subsitute '.' with '/' in you classpath input
     * and arive at the same output.
     * @return ClassPath that extends the <code>java.io.File</code>
     */
    public static File loadResource(String classPath) throws IOException
    {
        
        return new File(getURI(classPath));

    }
    
    /**
     * Factory method for loading a given class path resource into a 
     * <code>java.io.File</code>.  If an IOException is thrown it handles it 
     * with a run time exception (silently).
     * 
     * @param classPath java class path<br>
     * <strong>EXAMPLE:</strong><br>
     * "com.yourModifiedPackage.yourClass.java"<br> 
     * "com.yourModifiedPackage.yourXML.xml<br><br>
     * <strong>NOTE:</strong>Alternativly you can subsitute '.' with '/' in you classpath input
     * and arive at the same output.
     * @return ClassPath that extends the <code>java.io.File</code>
     */
    public static ClassPath loadResourceSilently(String classPath){
        try{
            
            return new ClassPath(getURI(classPath));
        }catch (Exception ex){
            throw new RuntimeException("A method marked as silent threw and exception.  \n\r Caused By: " + ex.getMessage());
        }
    }
    
    private ClassPath(URI uri)
    {
        super(uri);
    }
    
    private ClassPath()
    {
        super("");
    }
    
    public static final URL getURL(Class aClass){
                  
            return aClass.getResource(aClass.getCanonicalName());
            
    }
    
    private static final URI getURI(String classPath) throws IOException
    {
        try
        {
            
            URL url = ClassPath.class.getResource(classPath);
            return url.toURI();
        }
        catch(Exception ex)
        {
            throw new FileNotFoundException("The file for classpath " + classPath + " does not exists.  Check that the file exists and the format of the given classpath.");
        }
    }

}
